package com.zr.common.export;

import com.zr.entity.Staff;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 生成excel工具
 */
public class ExcelGenerator {

	
	public static ByteArrayInputStream customersToExcel(List<Staff> customers) throws IOException {
		String[] COLUMNs = {"姓名", "性别", "工号", "lob工号","事业部", "交付部", "级别", "年龄","毕业日期", "学历", "手机号码", "紧急联系人","紧急联系电话"};
		try(
                Workbook workbook = new XSSFWorkbook();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
		){
			CreationHelper createHelper = workbook.getCreationHelper();

			Sheet sheet = workbook.createSheet("员工表");

			Font headerFont = workbook.createFont();
			headerFont.setBold(true);
			headerFont.setColor(IndexedColors.BLUE.getIndex());

			CellStyle headerCellStyle = workbook.createCellStyle();
			headerCellStyle.setFont(headerFont);

			// Row for Header
			Row headerRow = sheet.createRow(0);

			// Header
			for (int col = 0; col < COLUMNs.length; col++) {
				Cell cell = headerRow.createCell(col);
				cell.setCellValue(COLUMNs[col]);
				cell.setCellStyle(headerCellStyle);
			}

			// CellStyle for Age
			CellStyle ageCellStyle = workbook.createCellStyle();
			ageCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#"));

			int rowIdx = 1;
			for (Staff staffInfo : customers) {
				Row row = sheet.createRow(rowIdx++);

				row.createCell(0).setCellValue(staffInfo.getName());
				row.createCell(1).setCellValue(staffInfo.getSex() == 1 ? "男" : "女");
				row.createCell(2).setCellValue(staffInfo.getJobNumber());
				row.createCell(3).setCellValue(staffInfo.getLobJobNumber());
				row.createCell(4).setCellValue(staffInfo.getBusinessDept()   == 1 ? "是" : "否");
				row.createCell(5).setCellValue(staffInfo.getDeliveryDept()  == 1 ? "是" : "否");
				row.createCell(6).setCellValue(staffInfo.getLevel());
				row.createCell(7).setCellValue(staffInfo.getAge());
				row.createCell(8).setCellValue(new DateTime(staffInfo.getGraduationDate()).toString("yyyy年MM月dd日"));
				row.createCell(9).setCellValue(staffInfo.getEducationalBkg());
				row.createCell(10).setCellValue(staffInfo.getPhoneNumber());
				row.createCell(11).setCellValue(staffInfo.getEmergencyContact());
				row.createCell(12).setCellValue(staffInfo.getEmergencyPhoneNumber());

			}

			workbook.write(out);
			return new ByteArrayInputStream(out.toByteArray());
		}
	}
}